import { Controller, Get, Param } from '@nestjs/common';
import { errorReturn, successReturn } from 'src/util';
import { DeviceService } from './device.service';

@Controller('device')
export class DeviceController {
    constructor(
        private deviceService:DeviceService
    ) {}

    @Get(':deviceId')
    async device(@Param('deviceId') deviceId:number) {
        deviceId = Number(deviceId)
        if(!deviceId || deviceId <= 0) return errorReturn()
        try {
            const device = await this.deviceService.getDeviceById(deviceId)
            if(device) return successReturn(device)
        } catch (error) {
            console.error(error)
            return errorReturn(error.message)
        }

        return errorReturn('无设备信息')
    }

    @Get('strategy/:deviceId')
    async strategry(@Param('deviceId') deviceId:number) {
        deviceId = Number(deviceId)
        if(!deviceId || deviceId <= 0) return errorReturn()
        const strategy = await this.deviceService.getStrategyByDeviceId(deviceId)
        return successReturn(strategy)
    }
}
